<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\controller\article;

use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\article\ProcureLists;
use app\adminapi\logic\article\ArticleLogic;
use app\adminapi\logic\article\lpd;
use app\adminapi\validate\article\ArticleValidate;
use app\common\model\article\Article;
use app\common\model\article\Procure;
use app\common\model\qualifications\Qualifications;
use think\facade\Db;

/**
 * 资讯管理控制器
 * Class ArticleController
 * @package app\adminapi\controller\article
 */
class ArticleController extends BaseAdminController
{

    /**
     * @notes  查看资讯列表
     * @return \think\response\Json
     * @author heshihu
     * @date 2022/2/22 9:47
     */
    public function lists()
    {
        return $this->dataLists(new ProcureLists());
    }

    /**
     * @notes  添加资讯
     * @return \think\response\Json
     * @author heshihu
     * @date 2022/2/22 9:57
     */
    public function adds()
    {
        $params=$this->request->post();
        $params["user_id"]=(new Qualifications())->where("id","=",$params["q_id"])->value("user_id");
      $model=new Procure();

        $params["contacts"]=json_encode($params["contacts"]);
        $shuju=[
            'name' => $params['title'],
            'user_id' => $params['user_id'] ?? '',
            'contacts' => $params['contacts'] ?? '', //作者
            'sort' => $params['sort'] ?? 1000, // 排序
            'specifications' => $params['specifications'], // 文章摘要
            'producer' => $params['producer'] ?? 0,
            'fming' => $params['fming'],
            'start' => $params['start'],
            'ycxq'=>$params['ycxq'],
            'chuangjiantime'=>time()
        ];
        $app= new lpd();
        $rtn=  $app->run( $params['name']);
        $responseObject = json_decode($rtn);

        if ($responseObject->conclusion=='不合规'){
            return $this->success('操作失败', ['data' =>'字符违法' ], 0, 1);
        }
         $model->save($shuju);
           return $this->success('添加成功', ["data"=>1], 1, 1);



    }

    /**
     * @notes  编辑资讯
     * @return \think\response\Json
     * @author heshihu
     * @date 2022/2/22 10:12
     */
    public function edit()
    {
        $params = (new ArticleValidate())->post()->goCheck('edit');
        $result = ArticleLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(ArticleLogic::getError());
    }

    /**
     * @notes  删除资讯
     * @return \think\response\Json
     * @author heshihu
     * @date 2022/2/22 10:17
     */
    public function delete()
    {
        $params = $this->request->post();
        ArticleLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }

    /**
     * @notes  资讯详情
     * @return \think\response\Json
     * @author heshihu
     * @date 2022/2/22 10:15
     */
    public function detail()
    {
        $params =$this->request->get("id");
        $result = (new Procure())->where("id","=",$params)->append(['pada'])->findOrEmpty()->toArray();
        return $this->data($result);
    }

    public function alldelete()
    {
        $params = $this->request->post();

        $list= Db::name("procure")->whereIn("id",$params['idA'])->delete();

        return $this->success('删除成功',[$list=>1], 1, 1);
    }
    public function alltonguo()
    {
        $params = $this->request->post();

        $list= Db::name("procure")->whereIn("id",$params['idA'])->update(['start'=>1]);

        return $this->success('成功',[$list=>1], 1, 1);
    }
    public function allbutonguo()
    {
        $params = $this->request->post();

        $list= Db::name("procure")->whereIn("id",$params['idA'])->update(['start'=>0]);

        return $this->success('成功',[$list=>1], 1, 1);
    }
    /**
     * @notes  更改资讯状态
     * @return \think\response\Json
     * @author heshihu
     * @date 2022/2/22 10:18
     */
    public function updateStatus()
    {
       // $params = (new ArticleValidate())->post()->goCheck('status');
        $params = $this->request->post();
        $result = ArticleLogic::updateStatus($params);
        if (true === $result) {
            return $this->success('修改成功', [], 1, 1);
        }
        return $this->fail(ArticleLogic::getError());
    }


}